package tienmanh.gs.goldminer.scene;

import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.animator.IMenuAnimator;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.batch.SpriteGroup;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontLibrary;
import org.andengine.opengl.font.FontUtils;
import org.andengine.opengl.font.IFont;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;

import tienmanh.gs.goldminer.base.BaseScene;
import tienmanh.gs.goldminer.manager.SceneManager;
import tienmanh.gs.goldminer.manager.SceneManager.SceneType;
import tienmanh.gs.goldminer.util.Gold;
import tienmanh.gs.goldminer.util.Item;
import tienmanh.gs.goldminer.util.Star;


import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

//import org.andengine.util.adt.


public class ShopScene extends BaseScene implements IOnMenuItemClickListener
{
	private MenuScene menuChildScene;
	Gold gold = new Gold();
	Star star = new Star();
	Item item = new Item();
	
	long coint;
	int checkItem;
	long item1,item2,item3,item4;
	
	private Text txt_Gold,txt_Star;
	
	
//	private long coin;
	
	private final int MENU_START = 0;
	private final int MENU_MENU = 1;
	private final int MENU_MUA = 2;
	private final int MENU_BUY = 3;
	private IMenuItem playMuaItem;
	private IMenuItem playStartItem;
//	private IMenuItem playMenuItem;
	private IMenuItem playBuyItem1;
	private IMenuItem playBuyItem2;
	private IMenuItem playBuyItem3;
	private IMenuItem playBuyItem4;
	
	private int[] check ;
	private boolean[] isCheck;
	
	IMenuItem itemSelect[];
//	ITextureRegion itr[] ;
	
	private final int ITEM1 = 4;
	private final int ITEM2 = 5;
	private final int ITEM3 = 6;
	private final int ITEM4 = 7;
	
	private Text txt;
//	private Text txt_gold;
//	private Text txt_[];
	private Text txt_it1;
	private Text txt_it2;
	private Text txt_it3;
	private Text txt_it4;
	 Sprite arrown;
	
	public void creatItem(){
		
		 gold.init();
		 star.init();
		 item.init();
		 checkItem = 0;
		 
		
		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 0);
		
//		menuChildScene.attachChild(txt_gold);
		
		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);
		
		initItem();
		itemSelect = new IMenuItem[4];
		itemSelect[0] = new  ScaleMenuItemDecorator(new SpriteMenuItem(ITEM1, resourcesManager.textureRegionShopDiamond, vbom), 1.2f, 0.8f);
		itemSelect[1] = new  ScaleMenuItemDecorator(new SpriteMenuItem(ITEM2, resourcesManager.textureRegionShopMine, vbom), 1.2f, 0.7f);
		itemSelect[2] = new  ScaleMenuItemDecorator(new SpriteMenuItem(ITEM3, resourcesManager.textureRegionShopTime, vbom), 1.2f, 0.7f);
		itemSelect[3] = new  ScaleMenuItemDecorator(new SpriteMenuItem(ITEM4, resourcesManager.textureRegionShopStreng, vbom), 1.2f, 0.7f);
		
		check = new int[]{0,0,0,0};
		
		
		isCheck = new boolean[]{false,false,false,false};
		
		txt_Gold = new Text(114, 10, resourcesManager.font1, "",30, vbom);
	    txt_Gold.setColor(Color.YELLOW);
	    txt_Gold.setText(""+gold.readEGoldSP());
	    
	    coint = gold.readEGoldSP();
	    
	    txt_Star = new Text(350, 10, resourcesManager.font1, "",30, vbom);
	    txt_Star.setColor(Color.YELLOW);
	    txt_Star.setText(""+star.numberStar());
	    
	    txt_it1= new Text(305, 380, resourcesManager.font1, "",30, vbom);
	    txt_it1.setColor(Color.RED);
	    txt_it1.setText(""+item.readItemSP1());
	    
	    item1 = item.readItemSP1();
	    
	    txt_it2= new Text(375, 380, resourcesManager.font1, "",30, vbom);
	    txt_it2.setColor(Color.RED);
	    txt_it2.setText(""+item.readItemSP2());
	    
	    item2 = item.readItemSP2();
	    
	    txt_it3= new Text(450, 380, resourcesManager.font1, "",30, vbom);
	    txt_it3.setColor(Color.RED);
	    txt_it3.setText(""+item.readItemSP3());
	    
	    item3 = item.readItemSP3();
	    
	    txt_it4= new Text(525, 380, resourcesManager.font1, "",30, vbom);
	    txt_it4.setColor(Color.RED);
	    txt_it4.setText(""+item.readItemSP4());
	    
	    item4 = item.readItemSP4();
	    
	    txt= new Text(241, 268, resourcesManager.font1, "",100, vbom);
	    txt.setText("Băng : Đóng băng mọi thứ trong 15s \n       1500$");
	    
	    arrown = new Sprite(0, 0, resourcesManager.textureRegionArrown, vbom);
	    arrown.setScale(0.5f);
	    arrown.setPosition(145, 45);
		
		menuChildScene.attachChild(txt_Gold);
		menuChildScene.attachChild(txt_Star);
		menuChildScene.attachChild(txt_it1);
		menuChildScene.attachChild(txt_it2);
		menuChildScene.attachChild(txt_it3);
		menuChildScene.attachChild(txt_it4);
		menuChildScene.attachChild(txt);
		menuChildScene.attachChild(arrown);
		
		itemSelect[0].setPosition(109, 80);
		itemSelect[0].setScale(0.8f);
		itemSelect[1].setPosition(257, 80);
		itemSelect[1].setScale(0.7f);
		itemSelect[2].setPosition(405, 80);
		itemSelect[2].setScale(0.7f);
		itemSelect[3].setPosition(558, 80);
		itemSelect[3].setScale(0.7f);
		
		menuChildScene.addMenuItem(itemSelect[0]);
		menuChildScene.addMenuItem(itemSelect[1]);
		menuChildScene.addMenuItem(itemSelect[2]);
		menuChildScene.addMenuItem(itemSelect[3]);
		
		menuChildScene.setOnMenuItemClickListener(this);
		
		setChildScene(menuChildScene);
	}
	public void initItem(){
		playStartItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_START, resourcesManager.textureRegionNextLevel, vbom), 1.6f, 1);
		playMuaItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_MUA, resourcesManager.textureRegionBuy, vbom), 1.6f, 1);
//		playMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_MENU, resourcesManager.back_menu_region, vbom), 1.6f, 1);
		playBuyItem1 = new ScaleMenuItemDecorator(new SpriteMenuItem(-1, resourcesManager.textureRegionItemICE, vbom),1,1);
		playBuyItem2 = new ScaleMenuItemDecorator(new SpriteMenuItem(-1, resourcesManager.textureRegionItemTNT, vbom),1,1);
		playBuyItem3 = new ScaleMenuItemDecorator(new SpriteMenuItem(-1, resourcesManager.textureRegionItemCLOCK, vbom),1,1);
		playBuyItem4 = new ScaleMenuItemDecorator(new SpriteMenuItem(-1, resourcesManager.textureRegionItemMINE, vbom),1,1);
		
//		for (int i = 0; i < 4; i++) {
//			item[i]  = new  SpriteMenuItem(-1, itr1[i], vbom);
//			menuChildScene.addMenuItem(item[i]);
//		}
		
		menuChildScene.addMenuItem(playStartItem);
		menuChildScene.addMenuItem(playMuaItem);
//		menuChildScene.addMenuItem(playMenuItem);
		menuChildScene.addMenuItem(playBuyItem1);
		menuChildScene.addMenuItem(playBuyItem2);
		menuChildScene.addMenuItem(playBuyItem3);
		menuChildScene.addMenuItem(playBuyItem4);
		
		
//		item[0].setPosition(20,120);
//		item[1].setPosition(item[0].getX()+item[0].getWidth(), 120);
//		item[2].setPosition(20,120+ item[1].getHeight());
//		item[3].setPosition(item[2].getX()+item[2].getWidth(),120+ item[1].getHeight());
//		BuaItem.setPosition(150, 180);
		
		playStartItem.setPosition(716,6);
		playMuaItem.setPosition(357,323);
//		playMenuItem.setPosition(480 - playMuaItem.getWidth(),25);
		playBuyItem1.setPosition(260, 390);
		playBuyItem2.setPosition(333, 390);
		playBuyItem3.setPosition(407, 390);
		playBuyItem4.setPosition(482, 390);
//		txt_ = new Text[4];
//		txt_[0].setText("1000");
//		txt_[1].setText("1200");
//		txt_[2].setText("1800");
//		txt_[3].setText("1500");
//		
//		txt_[0].setPosition(110 , 250);
//		txt_[1].setPosition(txt_[0].getX()+item[0].getWidth() , 250);
//		txt_[2].setPosition(110 , 250+ item[1].getHeight());
//		txt_[3].setPosition(txt_[2].getX()+item[2].getWidth(),250+ item[1].getHeight());
	}
	
	public void creatBackground(){
		SpriteBackground background = new SpriteBackground(new Sprite(0, 0, resourcesManager.textureRegionShopBackground, vbom));
		setBackground(background);
	}
	
	
	
	@Override
	public void createScene()
	{
		creatBackground();
		creatItem();
//		getDataFromXMLFile();
	}

	@Override
	public void onBackKeyPressed()
	{
			SceneManager.getInstance().loadMapScene(engine);
	}

	@Override
	public SceneType getSceneType()
	{
		return SceneType.SCENE_SHOP;
	}

	@Override
	public void disposeScene()
	{
             
	}

	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
//		System.exit(0);
	}
	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
	}

	public void showToast(){
		resourcesManager.activity.toastOnUIThread("Bạn không đủ tiền mua vật phẩm này!!!");
	}
	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		// TODO Auto-generated method stub
		switch (pMenuItem.getID()) {
		case ITEM1:
			 txt.setText("Băng : Đóng băng mọi thứ trong 15s \n       1500$");
			 arrown.setPosition(145, 45);
			 checkItem = 0;
			break;
		case ITEM2:
			 txt.setText("Thuốc nổ : Phá hủy đồ vật đang kéo về \n       1000$");
			 arrown.setPosition(293, 45);
			 checkItem = 1;
			break;
		case ITEM3:
			 txt.setText("Đồng hồ : Tăng thêm 15s  \n       1200$");
			 arrown.setPosition(440, 45);
			 checkItem = 2;
			break;
		case ITEM4:
			 txt.setText("Sức mạnh : Giảm 50% kéo về \n       2000$");
			 arrown.setPosition(595, 45);
			 checkItem = 3;
			break;
		case MENU_MUA:
			switch (checkItem) {
			case 0:
				if(coint >= 1500){
					item1 ++;
					coint-=1500;
					gold.writeGoldSP(coint);
					txt_it1.setText(""+item1);
					txt_Gold.setText(""+gold.readEGoldSP());
					item.writeItemSP1(item1);
				}
				else
					showToast();
				break;
			case 1:
				if(coint >= 1000){
					item2 ++;
					coint-=1000;
					gold.writeGoldSP(coint);
					txt_it2.setText(""+item2);
					txt_Gold.setText(""+gold.readEGoldSP());
					item.writeItemSP2(item2);
				}
				else
					showToast();
				break;
			case 2:
				if(coint >= 1200){
					item3 ++;
					coint-=1200;
					gold.writeGoldSP(coint);
					txt_it3.setText(""+item3);
					txt_Gold.setText(""+gold.readEGoldSP());
					item.writeItemSP3(item3);
				}//
				else
					showToast();
				break;
			case 3:
				if(coint >= 2000){
					item4 ++;
					coint-=2000;
					gold.writeGoldSP(coint);
					txt_it4.setText(""+item4);
					txt_Gold.setText(""+gold.readEGoldSP());
					item.writeItemSP4(item4);
				}
				else
					showToast();
				break;

			default:
				break;
			}
			break;
		case MENU_START:
			SceneManager.getInstance().createLoadScene(engine);
			break;
		default:
			break;
		}
		return false;
	}

}